/**
 * @param {number[]} nums
 * @return {number}
 */
export var findMin = function (nums) {
  let [left, right, ans] = [0, nums.length - 1, nums[0]]
  while (left <= right) {
    const mid = (left + right) >> 1
    if (nums[left] <= nums[mid]) {
      // 说明左则有序
      ans = Math.min(ans, nums[left])
      left = mid + 1
    } else {
      // 说明右则有序
      ans = Math.min(ans, nums[mid])
      right = mid - 1
    }
  }
  return ans
}

/**
 * 找到右则第一个少于左则的最大值
 * @param {number[]} nums
 * @return {number}
 */
export var findMin2 = function (nums) {
  let [left, right] = [0, nums.length - 1]
  while (left < right) {
    const mid = (left + right) >> 1
    if (nums[mid] <= nums[right]) {
      // 说明右则有序
      right = mid
    } else {
      left = mid + 1
    }
  }
  return nums[right]
}
